package manager;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import pojo.PrisTeam1Car;
import pojo.PrisTeam1UserAccount;
import util.HibernateUtil;

public class CarManager {

	public boolean createAndStoreCar(PrisTeam1UserAccount user, String brand,
			String model, Integer price, String fuelType, Integer year,
			Integer power, Integer kilometers) {

		try {
			PrisTeam1Car car = new PrisTeam1Car();
			car.setPrisTeam1UserAccount(user);
			car.setBrand(brand);
			car.setModel(model);
			car.setPrice(price);
			car.setFuelType(fuelType);
			car.setYear(year);
			car.setPower(power);
			car.setKilometers(kilometers);
			car.setAddDate(new Date());
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.beginTransaction();
			session.save(car);
			session.getTransaction().commit();
			return true;

		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public PrisTeam1Car getCarById(String id) {
		int intId;
		if (!id.isEmpty())
			intId = Integer.parseInt(id);
		else
			return null;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Criteria crit = session.createCriteria(PrisTeam1Car.class);
		crit.add(Restrictions.like("id", intId));
		PrisTeam1Car car = (PrisTeam1Car) crit.uniqueResult();
		session.getTransaction().commit();

		return car;
	}

	public PrisTeam1Car getCar(String brand, String model, String price,
			String fuelType, Integer year, Integer power, Integer kilometers,
			Date date) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Criteria crit = session.createCriteria(PrisTeam1Car.class);
		if (!brand.isEmpty()) {
			crit.add(Restrictions.like("brand", brand));
		}
		if (!model.isEmpty()) {
			crit.add(Restrictions.like("model", model));
		}
		if (!price.isEmpty()) {
			crit.add(Restrictions.like("price", price));
		}
		if (!fuelType.isEmpty()) {
			crit.add(Restrictions.like("fuelType", fuelType));
		}
		if (year != null) {
			crit.add(Restrictions.like("year", year));
		}
		if (power != null) {
			crit.add(Restrictions.like("power", power));
		}
		if (kilometers != null) {
			crit.add(Restrictions.like("kilometers", kilometers));
		}
		if (date != null) {
			crit.add(Restrictions.like("date", date));
		}

		PrisTeam1Car car = (PrisTeam1Car) crit.uniqueResult();
		session.getTransaction().commit();

		return car;
	}

	@SuppressWarnings("unchecked")
	public List<PrisTeam1Car> searchCars(String brand, String model,
			Integer fromPrice, Integer toPrice, String fuelType,
			Integer fromYear, Integer toYear, Integer fromPower,
			Integer toPower, Integer fromKilometers, Integer toKilometers) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Criteria crit = session.createCriteria(PrisTeam1Car.class);
		if (!brand.isEmpty()) {
			crit.add(Restrictions.like("brand", brand));
		}
		if (!model.isEmpty()) {
			crit.add(Restrictions.like("model", model));
		}
		if (fromPrice != null && toPrice != null) {
			crit.add(Restrictions.between("price", fromPrice, toPrice));
		}
		if (!fuelType.isEmpty()) {
			crit.add(Restrictions.like("fuelType", fuelType));
		}
		if (fromYear != null && toYear != null) {
			crit.add(Restrictions.between("year", fromYear, toYear));
		}
		if (fromPower != null && toPower != null) {
			crit.add(Restrictions.between("power", fromPower, toPower));
		}
		if (fromKilometers != null && toKilometers != null) {
			crit.add(Restrictions.between("kilometers", fromKilometers,
					toKilometers));
		}

		List<PrisTeam1Car> cars = crit.list();
		session.getTransaction().commit();

		return cars;
	}

	public List<PrisTeam1Car> getAllCars() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		@SuppressWarnings("unchecked")
		List<PrisTeam1Car> allCars = (List<PrisTeam1Car>) session.createQuery(
				"from PrisTeam1Car").list();
		session.getTransaction().commit();

		return allCars;
	}

	@SuppressWarnings("unchecked")
	public List<PrisTeam1Car> getAds24() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH, -1);

		Criteria crit = session.createCriteria(PrisTeam1Car.class);
		crit.add(Restrictions.between("addDate", cal.getTime(), new Date()));

		List<PrisTeam1Car> cars = crit.list();
		session.getTransaction().commit();

		return cars;
	}

	public static void main(String[] args) {
		CarManager cm = new CarManager();
		PrisTeam1Car car = cm.getCarById("2");
		System.out.println(car.getBrand());
	}

}
